package com.qf.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsCollectMapper;
import com.qf.mapper.DtsFootprintMapper;
import com.qf.mapper.DtsUserAccountMapper;
import com.qf.mapper.DtsUserMapper;
import com.qf.pojo.*;
import com.qf.vo.DayStatis;
import org.apache.ibatis.jdbc.Null;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
@Service
public class DtsUserServiceImpl implements DtsUserService{
    //注入mapper
    @Autowired
    private DtsUserMapper dtsUserMapper;
    @Autowired
    private DtsUserAccountMapper dtsUserAccountMapper;
    @Autowired
    private DtsCollectMapper dtsCollectMapper;
    @Autowired
    private DtsFootprintMapper dtsFootprintMapper;


    //1分页全部用户列表
    @Override
    public List<DtsUser> findUser(int page,int limit,String sort,String order, String username,String mobile) {
        //自定义分页对象
        IPage<DtsUser> dtsUserIPage=new Page<DtsUser>(page,limit);

        QueryWrapper<DtsUser> queryWrapper=new QueryWrapper<>();
        //模糊查询用户名,手机号--排序方式
        queryWrapper.like(!StringUtils.isEmpty(username),"nickname",username);
        queryWrapper.like(!StringUtils.isEmpty(mobile),"mobile",mobile);
        queryWrapper.eq("deleted",0);
        queryWrapper.orderBy(!StringUtils.isEmpty(sort),"asc".equalsIgnoreCase(order),sort);
        //分页
        IPage<DtsUser> dtsUserIPage1 = dtsUserMapper.selectPage(dtsUserIPage,queryWrapper);

        List<DtsUser> dtsUserList = dtsUserIPage1.getRecords();
        return dtsUserList;
    }

    //1用户总数
    @Override
    public int userCount() {

        int count = dtsUserMapper.selectCount(null);

        return count;
    }
   //会员审批

    @Override
    public void approveAgency(int userid,int rate) {
        //1.通过userid获得用户
        DtsUser dtsUser = dtsUserMapper.selectOne(new LambdaQueryWrapper<DtsUser>()
                .eq(DtsUser::getId, userid));
        //修改状态  又3改为0
        dtsUser.setStatus(0);
        //修改
        dtsUserMapper.updateById(dtsUser);
        //2更新推广表的百分比
        //查询到推广表对象
        DtsUserAccount dtsUserAccount = dtsUserAccountMapper.selectOne(new LambdaQueryWrapper<DtsUserAccount>()
                .eq(DtsUserAccount::getUserId, userid));
        //赋值更新
        dtsUserAccount.setSettlementRate(rate);
        dtsUserAccountMapper.updateById(dtsUserAccount);
    }
    //代理推广
    @Override
    public DtsUserAccount detailApprove(int id) {
        DtsUserAccount dtsUserAccount = dtsUserAccountMapper.selectOne(new LambdaQueryWrapper<DtsUserAccount>()
                .eq(DtsUserAccount::getUserId, id));
        return dtsUserAccount;
    }

    //收藏总数
    @Override
    public int collectionCount() {
        return  dtsCollectMapper.selectCount(null);


    }
    //收藏列表
    @Override
    public List<DtsCollect> findCollection(int page, int limit, String sort, String order) {
        Page<DtsCollect> dtsCollectPage = dtsCollectMapper.selectPage(new Page<DtsCollect>(page, limit), new QueryWrapper<DtsCollect>());
        List<DtsCollect> dtsCollectList = dtsCollectPage.getRecords();
        return dtsCollectList;
    }
    //足迹总数
    @Override
    public int footprintCount() {
        return  dtsFootprintMapper.selectCount(null);
    }
    //足迹列表
    @Override
    public List<DtsFootprint> findFootprint(int page, int limit, String sort, String order) {
        Page<DtsFootprint> dtsFootprintPage = dtsFootprintMapper.selectPage(new Page<DtsFootprint>(page, limit), new QueryWrapper<DtsFootprint>());
        List<DtsFootprint> dtsFootprintsList = dtsFootprintPage.getRecords();
        return dtsFootprintsList;
    }

    /**
     * 日期对应的用户数
     * @param statisDaysRang
     * @return
     */
    @Override
    public List<DayStatis> findUserDayStatis(int statisDaysRang) {
        return dtsUserMapper.findUserDayStatis(statisDaysRang);
    }

    @Override
    public DtsUser finduser(int id) {
        return dtsUserMapper.selectOne(new QueryWrapper<DtsUser>().eq("id",id));
    }


}
